package org.Keks.example.Solitaire;

import java.awt.Canvas;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import org.Keks.KeksGame;
import org.Keks.KeksMap;
import org.Keks.graphics.*;
import org.Keks.input.*;

/**
 * Our actual scene where the gamelogic is in action.
 * This is actually the last chance to define our rendering priority.
 * @author kevin
 *
 */
public class SpritesMap extends KeksMap implements MouseListener, MouseMotionListener  {
	public int mouseX, mouseY, randomcard;
	
	public KeksBitmap[] cardsEnum = {
			SpriteRegister.kr_a[0][0], SpriteRegister.kr_2[0][0], SpriteRegister.kr_3[0][0], SpriteRegister.kr_4[0][0],
			SpriteRegister.ka_a[0][0], SpriteRegister.ka_2[0][0], SpriteRegister.ka_3[0][0], SpriteRegister.ka_4[0][0], 
			SpriteRegister.p_a[0][0], SpriteRegister.p_2[0][0], SpriteRegister.p_3[0][0], SpriteRegister.p_4[0][0], 
			SpriteRegister.h_a[0][0], SpriteRegister.h_2[0][0], SpriteRegister.h_3[0][0], SpriteRegister.h_4[0][0]
		};
	public List<Integer> availableCards = new ArrayList<Integer>();
	public List<Integer> drawCardStack = new ArrayList<Integer>();
	public List<Integer> cardStack01 = new ArrayList<Integer>();
	public List<Integer> cardStack02 = new ArrayList<Integer>();
	public List<Integer> cardStack03 = new ArrayList<Integer>();
	public List<Integer> cardStack04 = new ArrayList<Integer>();
	public List<Integer> cardStack05 = new ArrayList<Integer>();
	public List<Integer> cardStack06 = new ArrayList<Integer>();
	public List<Integer> cardStack07 = new ArrayList<Integer>();
	
	public KeksBitmap background;
	private String debugio = "";

	public SpritesMap(KeksKeys k, Component sb) {
		super(k, sb);
		for(int i = 0; i<16; i++) {
			availableCards.add(i);
		}
		
		generateCardStack((ArrayList<Integer>)cardStack01, 1);
		generateCardStack((ArrayList<Integer>)cardStack02, 2);
		generateCardStack((ArrayList<Integer>)cardStack03, 3);
		generateCardStack((ArrayList<Integer>)cardStack04, 4);
		generateCardStack((ArrayList<Integer>)cardStack05, 5);
		
		debugio = "DrawCardStack used ";
		for(int j = 0; j<availableCards.size(); j++) {
			while(availableCards.size() > 0) {
				randomcard = (int) (Math.random()*16);
				for(int i = 0; i<availableCards.size(); i++) {
					if(availableCards.get(i) == randomcard) {
						availableCards.remove(i);
						drawCardStack.add(randomcard);
						debugio = debugio+""+randomcard+" ";
						break;
					} else {
						continue;
					}
				}
			}
		}
		System.out.println(debugio);
	}
	
	private void generateCardStack(ArrayList<Integer> stack, int size) {
		debugio = "CardStack0"+size+" used ";
		for(int j = 0; j<size; j++) {
			boolean foundCard = false;
			while(foundCard == false) {
				randomcard = (int) (Math.random()*16);
				for(int i = 0; i<availableCards.size(); i++) {
					if(availableCards.get(i) == randomcard) {
						availableCards.remove(i);
						stack.add(randomcard);
						debugio = debugio+""+randomcard+" ";
						foundCard = true;
						break;
					} else {
						continue;
					}
				}
			}
		}
		System.out.println(debugio);
	}
	
	public void init() {
		//gframe.addMouseMotionListener(this);
	}
	
	public void tick() {
	}
	
	public void render(KeksScreen screen) {
		super.render(screen);
		background = new KeksBitmap(KeksGame.GAME_WIDTH, KeksGame.GAME_HEIGHT);
		background.clear(0x88FFFFFF);
		//background.clear(0xFF003300);
		screen.blit(background);
		
		screen.blit(SpriteRegister.deck[0][0], 10, 10);
		screen.blit(SpriteRegister.cardhold[0][0], 83, 10);
		KeksBitmap card = null;
		card = cardsEnum[drawCardStack.get(drawCardStack.size()-1)];
		screen.blit(card, 83, 10);
		screen.blit(SpriteRegister.cardhold[0][0], 229, 10);
		screen.blit(SpriteRegister.cardhold[0][0], 302, 10);
		screen.blit(SpriteRegister.cardhold[0][0], 375, 10);
		screen.blit(SpriteRegister.cardhold[0][0], 448, 10);
		if(cardStack01.size() > 0) {
			renderCardStack(screen,(ArrayList<Integer>)cardStack01, cardStack01.size(), 1);
		} else {
			screen.blit(SpriteRegister.cardhold[0][0], 10, 100);
		}
		if(cardStack02.size() > 0) {
			renderCardStack(screen,(ArrayList<Integer>)cardStack02, cardStack02.size(), 2);
		} else {
			screen.blit(SpriteRegister.cardhold[0][0], 83, 100);
		}
		if(cardStack03.size() > 0) {
			renderCardStack(screen,(ArrayList<Integer>)cardStack03, cardStack03.size(), 3);
		} else {
			screen.blit(SpriteRegister.cardhold[0][0], 156, 100);
		}
		if(cardStack04.size() > 0) {
			renderCardStack(screen,(ArrayList<Integer>)cardStack04, cardStack04.size(), 4);
		} else {
			screen.blit(SpriteRegister.cardhold[0][0], 229, 100);
		}
		if(cardStack05.size() > 0) {
			renderCardStack(screen,(ArrayList<Integer>)cardStack05, cardStack05.size(), 5);
		} else {
			screen.blit(SpriteRegister.cardhold[0][0], 302, 100);
		}
		if(cardStack06.size() > 0) {
			renderCardStack(screen,(ArrayList<Integer>)cardStack06, cardStack06.size(), 6);
		} else {
			screen.blit(SpriteRegister.cardhold[0][0], 375, 100);
		}
		if(cardStack07.size() > 0) {
			renderCardStack(screen,(ArrayList<Integer>)cardStack07, cardStack07.size(), 7);
		} else {
			screen.blit(SpriteRegister.cardhold[0][0], 448, 100);
		}
	}
	
	private void renderCardStack(KeksScreen screen, ArrayList<Integer> stack, int size, int id) {
		KeksBitmap card = null;
		for(int i = 0; i<stack.size(); i++) {
			card = cardsEnum[stack.get(i)];
			if(i == stack.size()-1) {
				screen.blit(card,10+(70*(id-1)), 100+(15*i));
			} else {
				screen.blit(SpriteRegister.deck[0][0],10+(70*(id-1)), 100+(15*i));
			}
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

}
